package com.aiyan.edu.service.user.center.service;

import com.aiyan.edu.framework.domain.ucenter.CompanyUser;
import com.aiyan.edu.framework.domain.ucenter.Menu;
import com.aiyan.edu.framework.domain.ucenter.User;
import com.aiyan.edu.framework.domain.ucenter.ext.UserExt;
import com.aiyan.edu.framework.model.response.CommonCode;
import com.aiyan.edu.framework.model.response.CommonResponseResult;
import com.aiyan.edu.service.user.center.dao.CompanyUserRepository;
import com.aiyan.edu.service.user.center.dao.MenuMapper;
import com.aiyan.edu.service.user.center.dao.UserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: zqzhou
 * @create: 2019/11/21 23:57
 **/
@Service
public class UserService {

    @Autowired
    UserRepository userRepository;

    @Autowired
    CompanyUserRepository companyUserRepository;

    @Autowired
    MenuMapper menuMapper;

    /**
     * 查询用户信息
     *
     * @param username
     * @return
     */
    public CommonResponseResult<UserExt> getUserExt(String username) {
        User user = this.userRepository.findByUsername(username);
        if (null == user) {
            return new CommonResponseResult<>(CommonCode.SUCCESS, null);
        }

        UserExt userExt = new UserExt();
        BeanUtils.copyProperties(user, userExt);

        CompanyUser companyUser = this.companyUserRepository.findByUserId(user.getId());
        if (null != companyUser) {
            userExt.setCompanyId(companyUser.getCompanyId());
        }

        List<Menu> permissions = this.menuMapper.findPermissionsByUserId(user.getId());

        userExt.setPermissions(permissions);

        return new CommonResponseResult<>(CommonCode.SUCCESS, userExt);
    }
}
